layui.define(['element', 'layer',"upload", 'form', 'eleTree', 'common', 'jquery', 'laydate','textool','layedit'], function (exports) {
    var $ = layui.jquery
        , layer = layui.layer
        , form = layui.form
        , laydate = layui.laydate
        , upload = layui.upload
        , layedit=layui.layedit
        , eleTree = layui.eleTree
        , common = layui.common;
    //初始传入参数
    var _config = {};
    var layeditIndex;
    window.listData = [{t: 1}];
    var view = {
        init: function (config) {
            _config = config;

            layedit.set({
                //暴露layupload参数设置接口 --详细查看layupload参数说明
                uploadImage: {
                    url: '/common/layedit/upload',
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: '10240',
                    done: function(res, index, upload){ //上传后的回调

                    }
                }
                , uploadVideo: {
                    url: '/common/layedit/upload',
                    accept: 'video',
                    acceptMime: 'video/*',
                    exts: 'mp4|flv|avi|rm|rmvb',
                    size: '20480'
                }
                //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
                //传递参数：
                //图片： imgpath --图片路径
                //视频： filepath --视频路径 imgpath --封面路径
                , calldel: {
                    url: '/common/layedit/remove'
                }
                //开发者模式 --默认为false
                , devmode: true
                //插入代码设置
                , codeConfig: {
                    hide: true,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }
                , tool: [
                    'html', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'colorpicker', 'face'
                    , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'image_alt', 'video', 'anchors'
                    , '|', 'fullScreen'
                ]
                , height: '90%'
            });
            form.render();
            layeditIndex = layedit.build('content'); //建立编辑器
            //上传
            upload.render({
                elem: '#uploader',
                url: '/mediaFile/addMateria',
                data:{type:'thumb'},
                size: 64,
                accept:"images",
                exts: 'JPG|jpg',
                before: function(){
                    showLoading();
                },
                done: function (result) {
                    layer.closeAll('loading');
                    if (result.success) {
                        $("#uploader").html("<img src='" + result.data.url + "' style='width: 150px;height: 150px;'/><p>点击重新上传，或将文件拖拽到此处</p>");
                        $("#add_picpath").val(result.data.url);
                        $("#add_thumbMediaId").val(result.data.imgMediaId);
                        var t = $("#add_form").attr("data-t");
                        $(".wxmp-doclist div[data-t=" + t + "] img").attr("src", result.data.url);
                    } else {
                        layer.msg(result.msg || "上传接口异常");
                    }
                },
                error: function(){
                    layer.closeAll('loading');
                    layer.msg(result.msg || "上传接口异常");
                }
            });



            //开启关闭留言
            form.on('switch(comment)', function(data){
                if(data.elem.checked){
                    $("#add_comment_type").show();
                }else{
                    $("#add_comment_type").hide();
                }
            });




            form.on('submit(btn-add)', function (d) {
                var formData = d.field;
                var data = {};
                data.multType=1;//这里指定死
                data.title = formData.title;
                data.author = formData.author;

                data.thumbMediaId = formData.thumbMediaId;
                data.picpath = formData.picpath;
                data.showpic = formData.showpic?1:0;

                data.brief = formData.brief;
                data.fromurl = formData.fromurl;
                data.opencomment=formData.open_comment?1:0;
                data.fanscancomment=formData.fans_can_comment;
                data.description = layedit.getContent(layeditIndex);
                if (data.thumbMediaId == "") {
                    layer.msg("请上传封面图");
                    return false;
                }
                if (data.description == "") {
                    layer.msg("请输入文章内容");
                    return false;
                }
                ajax({
                    url: '/msgnews/addSingleNews',
                    data: data,
                    success: function (result) {
                        if (result.code>0){
                            layer.msg(result.msg);
                        }else {
                            layer.msg("保存成功");
                            setTimeout(function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.layui.list.refreshTable();
                            }, 1000);
                        }
                    },
                });
                return false;
            });

            //同步标题
            $("#add_tltle").keyup(function () {
                var v = $(this).val();
                $(".wxmp-doclist .main p").html(v ? v : "请输入标题")
            });
        },
        submit: function (config) {
            var iframeWindow = window['layui-layer-iframe' + config.index];
            var _form = $(window.frames["layui-layer-iframe" + config.index].document).contents().find("#btn-add");
            $(_form).click();
        }
    };
    exports("addsingle", view);
});